#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#define BUFSIZE 1024
#define STR "Hello world\n"
int main(int argc,char *argv)
{
    int        fd=-1;
    int        rv=-1;
    char       buf[BUFSIZE];                 //buf用于接受字符


    fd=open("hello.t",O_RDWR,0666);
    if(fd<0)
    {
      perror("Open file text.c failure");
      return 0;
    }

    printf("Open file returned file descriptor:%d\n",fd);

    if( (rv=write(fd,STR,strlen(STR) ) )<0 )
    {
      printf("Write %d bytes into file failure:%s\n",rv,strerror(errno));
      goto cleanup;
    }

    //Read 不出字节不是权限不够，而是未调用lseek
    lseek(fd,0,SEEK_SET);
    if(access("hello.t",R_OK) !=0)
    {
	    printf("Read hello.t failure\n");

    }
    if(access("hello.t",W_OK) !=0)
    {
            printf("Wead hello.t failure\n");

    }

    memset(buf,0,sizeof(buf));                   //初始化buf为0
    if( (rv =read(fd,buf,sizeof(buf) )) <0 )
    {	    
      printf("Read data from file failure:%s\n",strerror(errno));
      goto cleanup;
    }

    printf("Read %d bytes data from file:%s\n",rv,buf);

cleanup:
    close(fd);

    return 0;



}
